package com.wsh.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsh.dto.sys.DictTypeDto;
import com.wsh.entity.sys.DictEntity;
import com.wsh.entity.sys.DictTypeEntity;
import com.wsh.mapper.sys.DictTypeDao;
import com.wsh.utils.PageUtils;
import com.wsh.sys.DictService;
import com.wsh.sys.DictTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 王思涵
 * @Date 2019/8/10
 * @Version 1.0
 * @Desciption
 **/
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeDao, DictTypeEntity> implements DictTypeService {

    @Autowired
    private DictService dictService;

    @Override
    public IPage<DictTypeEntity> getList(DictTypeDto dictTypeDto) {
        // 分页数据处理
        IPage<DictTypeEntity> pages = PageUtils.getPage(dictTypeDto);

        String typeValue = dictTypeDto.getTypeValue();
        String name = dictTypeDto.getName();

        // 分页查询处理
        IPage<DictTypeEntity> page =  baseMapper.selectPage(pages, new QueryWrapper<DictTypeEntity>()
                .like(StringUtils.isNotBlank(typeValue), "type_value", typeValue)
                .eq(StringUtils.isNotBlank(name), "name", name)
                        .orderByAsc("dict_type_order"));

        return page;
    }

    @Override
    public void delDictType(String id) {
        // 1 查询当前字典类型
        DictTypeEntity dictTypeEntity = getById(id);

        // 2 查询当前所有子类型
        if (dictTypeEntity != null) {
            List<DictEntity> dictEntityList = dictService.getDictByType(id);

            // 3 删除子类型 字典类型
            if (dictEntityList != null && dictEntityList.size() > 0) {
                dictService.removeByIds(dictEntityList.stream().map(DictEntity::getId).collect(Collectors.toList()));
            }

            removeById(id);
        }

    }
}
